package com.epro.dx.adapter;

import java.io.InputStream;
import java.util.List;

import com.epro.dx.R;
import com.weipu.common.constants.Constant;
import com.weipu.common.facade.model.ProductModel;
import com.weipu.common.util.Logger;
import com.weipu.common.util.NetWorkUtil;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.epro.dx.R;
import com.weipu.common.facade.model.ProductModel;
import com.weipu.common.util.NetWorkUtil;

import java.io.InputStream;
import java.util.List;

/**
 *  适配器基类
 * 
 * @author cwjtojava
 * @param <T>
 */
@SuppressWarnings("ResourceType")
public abstract class CommonAdapter<T> extends BaseAdapter
{
    private List<T> list;
    
    private Context context;
    
    public Bitmap defaultBitmap;
    
    protected boolean isWifi;
    
    public CommonAdapter(List<T> list, Context context)
    {
        this.list = list;
        this.context = context;
        this.isWifi=NetWorkUtil.isWifi(context);
    }
    
    
    @Override
    public int getCount()
    {
        return list.size();
    }
    
    @Override
    public Object getItem(int position)
    {
    	if(null!=list&&list.size()!=0){
    		return list.get(position);
    	}else{
    		return null;
    	}
        
    }
    
    @Override
    public long getItemId(int position)
    {
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        return getView(position, convertView, list, context);
    }
    
    /**
     * 根据网络连接状态及屏幕像素密度获取图片URL地址
     * @param densityDpi
     * @param product
     * @param defaultDpi1
     * @param defaultDpi2
     * @return
     */
    protected String[] getImageUrl(int densityDpi,ProductModel product,int defaultDpi1,int defaultDpi2){
    	String[] productIconUrl=null;
    	if(densityDpi>=defaultDpi1){
			if(isWifi){
				 productIconUrl = product.getMediumPictureURL();
			}else{
				 if(densityDpi>=defaultDpi2){
					 productIconUrl = product.getMediumPictureURL();
				 }else{
					 productIconUrl = product.getSmallPictureURL();
				 }
			}
			
		}else{
			 productIconUrl = product.getSmallPictureURL();
		}
    	return productIconUrl;
    }
    
	
	/**
	 * 获取默认图片
	 * @param context
	 */
	 public Bitmap getDefault243(Context context)
	 {
		 if(null==defaultBitmap){
			 InputStream is = context.getResources().openRawResource(R.drawable.item_default);
			 final BitmapFactory.Options options = new BitmapFactory.Options();
		     options.inSampleSize=1;
		     defaultBitmap =BitmapFactory.decodeStream(is,null,options); 
		 }
		 return defaultBitmap;
	 }
    
    public abstract View getView(int position, View convertView, List<T> list, Context context);
}
